﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Dynamic;
using System.Text;
using System.Threading.Tasks;
using Abp;
using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.AutoMapper;
using Abp.Configuration;
using Abp.Domain.Repositories;
using Abp.Extensions;
using Abp.Linq.Extensions;
using System.Linq;
using System.Linq.Dynamic.Core;
using KDS.Mes.Mes.TestSlides.Dtos;
using KDS.Mes.Dto;
using KDS.Mes.Authorization;




namespace KDS.Mes.Mes.TestSlides
{
    /// <summary>
    /// 滑道检测数据表服务实现
    /// </summary>
    [AbpAuthorize(AppPermissions.Pages_Report_TestSlides)]


    public class TestSlideAppService : MesAppServiceBase, ITestSlideAppService
    {
        private readonly IRepository<TestSlide, int> _testSlideRepository;
        private readonly ITestSlideListExcelExporter _testSlideListExcelExporter;


        private readonly TestSlideManage _testSlideManage;
        /// <summary>
        /// 构造方法
        /// </summary>
        public TestSlideAppService(IRepository<TestSlide, int> testSlideRepository,
TestSlideManage testSlideManage
      , ITestSlideListExcelExporter testSlideListExcelExporter
  )
        {
            _testSlideRepository = testSlideRepository;
            _testSlideManage = testSlideManage;
            _testSlideListExcelExporter = testSlideListExcelExporter;
        }


        #region 实体的自定义扩展方法
        private IQueryable<TestSlide> _testSlideRepositoryAsNoTrack => _testSlideRepository.GetAll().AsNoTracking();


        #endregion


        #region 滑道检测数据表管理

        public PagedResultDto<TestSlideListDto> GetPagedTestSlides(GetTestSlideInput input)
        {
            //初步过滤
            var query = _testSlideRepository.GetAll()
                        .WhereIf(!input.SeatID.IsNullOrEmpty(), u => u.SEAT_ID == input.SeatID)
                        .WhereIf(!input.LineId.IsNullOrEmpty(), u => u.LINE == input.LineId)
                        .WhereIf(!input.Time1.ToString().IsNullOrEmpty(), u => u.CREAT_TIME >= input.Time1)
                        .WhereIf(!input.Time2.ToString().IsNullOrEmpty(), u => u.CREAT_TIME <= input.Time2);
            //排序
            query = !string.IsNullOrEmpty(input.Sorting) ? query.OrderBy(x => x.Id) : query.OrderByDescending(t => t.CREAT_TIME);
            //获取总数
            var tasksCount = query.Count();
            //默认的分页方式
            //var taskList = query.Skip(input.SkipCount).Take(input.MaxResultCount).ToList();
            //ABP提供了扩展方法PageBy分页方式
            var taskList = query.PageBy(input).ToList();
            return new PagedResultDto<TestSlideListDto>(tasksCount, taskList.MapTo<List<TestSlideListDto>>());
        }


        /// <summary>
        /// 根据查询条件获取滑道检测数据表分页列表
        /// </summary>
        public async Task<PagedResultDto<TestSlideListDto>> GetPagedTestSlidesAsync(GetTestSlideInput input)
        {

            var query = _testSlideRepositoryAsNoTrack;
            //TODO:根据传入的参数添加过滤条件

            var testSlideCount = await query.CountAsync();

            var testSlides = await query
            .OrderBy(input.Sorting)
            .PageBy(input)
            .ToListAsync();

            var testSlideListDtos = testSlides.MapTo<List<TestSlideListDto>>();
            return new PagedResultDto<TestSlideListDto>(
            testSlideCount,
            testSlideListDtos
            );
        }

        /// <summary>
        /// 通过Id获取滑道检测数据表信息进行编辑或修改 
        /// </summary>
        public async Task<GetTestSlideForEditOutput> GetTestSlideForEditAsync(NullableIdDto<int> input)
        {
            var output = new GetTestSlideForEditOutput();

            TestSlideEditDto testSlideEditDto;

            if (input.Id.HasValue)
            {
                var entity = await _testSlideRepository.GetAsync(input.Id.Value);
                testSlideEditDto = entity.MapTo<TestSlideEditDto>();
            }
            else
            {
                testSlideEditDto = new TestSlideEditDto();
            }

            output.TestSlide = testSlideEditDto;
            return output;
        }


        /// <summary>
        /// 通过指定id获取滑道检测数据表ListDto信息
        /// </summary>
        public async Task<TestSlideListDto> GetTestSlideByIdAsync(EntityDto<int> input)
        {
            var entity = await _testSlideRepository.GetAsync(input.Id);

            return entity.MapTo<TestSlideListDto>();
        }







        /// <summary>
        /// 新增或更改滑道检测数据表
        /// </summary>
        public async Task CreateOrUpdateTestSlideAsync(CreateOrUpdateTestSlideInput input)
        {
            if (input.TestSlideEditDto.Id.HasValue)
            {
                await UpdateTestSlideAsync(input.TestSlideEditDto);
            }
            else
            {
                await CreateTestSlideAsync(input.TestSlideEditDto);
            }
        }

        /// <summary>
        /// 新增滑道检测数据表
        /// </summary>
        //[AbpAuthorize(AppPermissions.Pages_Report_TestSlides_Create)]
        public virtual async Task<TestSlideEditDto> CreateTestSlideAsync(TestSlideEditDto input)
        {
            //TODO:新增前的逻辑判断，是否允许新增

            var entity = input.MapTo<TestSlide>();

            entity = await _testSlideRepository.InsertAsync(entity);
            return entity.MapTo<TestSlideEditDto>();
        }

        /// <summary>
        /// 编辑滑道检测数据表
        /// </summary>
        //[AbpAuthorize(AppPermissions.Pages_Report_TestSlides_Edit)]
        public virtual async Task UpdateTestSlideAsync(TestSlideEditDto input)
        {
            //TODO:更新前的逻辑判断，是否允许更新

            var entity = await _testSlideRepository.GetAsync(input.Id.Value);
            input.MapTo(entity);

            await _testSlideRepository.UpdateAsync(entity);
        }

        /// <summary>
        /// 删除滑道检测数据表
        /// </summary>
        //[AbpAuthorize(AppPermissions.Pages_Report_TestSlides_Delete)]
        public async Task DeleteTestSlideAsync(EntityDto<int> input)
        {
            //TODO:删除前的逻辑判断，是否允许删除
            await _testSlideRepository.DeleteAsync(input.Id);
        }

        /// <summary>
        /// 批量删除滑道检测数据表
        /// </summary>
        //[AbpAuthorize(AppPermissions.Pages_Report_TestSlides_Delete)]
        public async Task BatchDeleteTestSlideAsync(List<int> input)
        {
            //TODO:批量删除前的逻辑判断，是否允许删除
            await _testSlideRepository.DeleteAsync(s => input.Contains(s.Id));
        }

        #endregion
        #region 滑道检测数据表的Excel导出功能


        public async Task<FileDto> GetTestSlideToExcel(GetTestSlideInput input)
        { 
            var entities = await _testSlideRepository.GetAll()
                 .WhereIf(!input.SeatID.IsNullOrEmpty(), u => u.SEAT_ID == input.SeatID)
                       .WhereIf(!input.LineId.IsNullOrEmpty(), u => u.LINE == input.LineId)
                       .WhereIf(!input.Time1.ToString().IsNullOrEmpty(), u => u.CREAT_TIME >= input.Time1)
                       .WhereIf(!input.Time2.ToString().IsNullOrEmpty(), u => u.CREAT_TIME <= input.Time2)
                .ToListAsync();


            var dtos = entities.MapTo<List<TestSlideListDto>>();

            var fileDto = _testSlideListExcelExporter.ExportTestSlideToFile(dtos);



            return fileDto;
        }


        #endregion










    }
}
